#include <gflags/gflags.h>

#include "log.h"
#include "user_server.h"

// log
DEFINE_int32(log_level, 1, "日志等级");
DEFINE_bool(mode, false, "调试模式-false, 发布模式-true");
DEFINE_string(path, "./logfile", "发布模式下日志输出到的文件路径");

// etcd
DEFINE_string(etcd_host, "http://127.0.0.1:2379", "etcd服务器地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(instance_name, "/user/instance", "当前实例名称");
DEFINE_string(access_host, "127.0.0.1:8083", "当前实例的外部访问地址");

// brpc
DEFINE_int32(brpc_port, 8083, "监听端口");
DEFINE_int32(idle_timeout_sec, -1, "RPC服务连接超时时间");
DEFINE_int32(num_threads, 1, "RPC服务线程个数");

// odb
DEFINE_string(odb_ip, "127.0.0.1", "mysql服务器ip");
DEFINE_uint32(odb_port, 0, "端口");
DEFINE_string(user, "root", "mysql用户");
DEFINE_string(password, "123", "对应用户的密码");
DEFINE_string(odb_db, "chat", "连接的mysql数据库");
DEFINE_string(charset, "utf8", "字符集");
DEFINE_int32(max_pool, 3, "Mysql连接池最大连接数量");

// redis
DEFINE_string(redis_ip, "127.0.0.1", "这是服务器的IP地址，格式：127.0.0.1");
DEFINE_int32(redis_port, 6379, "这是服务器的端口, 格式: 8080");
DEFINE_int32(redis_db, 0, "库的编号: 默认0号");
DEFINE_bool(keep_alive, true, "是否进行长连接保活");

// es
DEFINE_string(es_host, "http://127.0.0.1:9200/", "es服务器URL");

int main(int argc, char* argv[]) {
    google::ParseCommandLineFlags(&argc, &argv, true);
    chat::initLogger(FLAGS_log_level, FLAGS_mode, FLAGS_path);

    chat::UserServerBuilder user_builder;
    user_builder.makeServiceManager("/service/file");
    user_builder.makeDiscoveryClient(FLAGS_etcd_host, FLAGS_base_service);
    user_builder.makeEsClient({FLAGS_es_host});
    user_builder.makeOdbClient(FLAGS_max_pool, FLAGS_user, FLAGS_password,
                               FLAGS_odb_db, FLAGS_odb_ip, FLAGS_odb_port,
                               FLAGS_charset);
    user_builder.makeRedisClient(FLAGS_redis_ip, FLAGS_redis_port,
                                 FLAGS_redis_db, FLAGS_keep_alive);
    user_builder.makeSMSClient("6dwezjV9PMmgLkRG");

    user_builder.makeBrpcServer(FLAGS_brpc_port, FLAGS_idle_timeout_sec,
                                FLAGS_num_threads);
    user_builder.makeRegisterClient(FLAGS_base_service + FLAGS_instance_name,
                                    FLAGS_etcd_host, FLAGS_access_host);

    std::shared_ptr<chat::UserServer> user_server = user_builder.build();

    user_server->start();

    return 0;
}